package com.zr.action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.Content;
import com.zr.model.User;
import com.zr.service.ContentService;
import com.zr.service.UserService;
import com.zr.service.impl.ContentServiceImpl;
import com.zr.service.impl.UserServiceImpl;

public class LoginAction extends HttpServlet{

	UserService us = new UserServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf8");
		req.setCharacterEncoding("utf8");
		String uname = req.getParameter("uname");
		String upsw = req.getParameter("upsw");
		String check = req.getParameter("check").toUpperCase();
		if(!req.getSession().getAttribute("piccode").equals(check)){
			req.setAttribute("loginMsg", "验证码错误！");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}else{
			User user = us.getUserByUnameAndUpsw(uname, upsw);
			if(user.getUname()==null){
				req.setAttribute("loginMsg", "用户名或密码错误！");
				req.getRequestDispatcher("login.jsp").forward(req, resp);
			}else if(user.getRid()==2){
				req.getSession().setAttribute("user", user);
				ContentService cs = new ContentServiceImpl();
				/*ArrayList<Content> list = cs.getMaxNumberContent();
		 		HttpSession session = req.getSession();		
				session.setAttribute("maxContent2", list);*/
				req.getSession().setAttribute("data", cs.getData());
				req.getRequestDispatcher("show.jsp").forward(req, resp);
			}else{
				req.setAttribute("loginMsg", "您不是知识管理员！");
				req.getRequestDispatcher("login.jsp").forward(req, resp);
			}
		}
	}
	
}
